import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../common/prisma/prisma.service';
import {
  CategoryItem,
  CategoryResponse,
} from './interfaces/category.interface';

@Injectable()
export class CategoryService {
  constructor(private prisma: PrismaService) {}

  // 统一处理分类数据格式
  private formatCategoryData(data: any, level: number): CategoryItem {
    return {
      id: data[`level_${level}_id`],
      code: data[`level_${level}_code`],
      name: data[`level_${level}_name`],
      level,
    };
  }

  async getLevel1Categories(): Promise<CategoryResponse> {
    const items = await this.prisma.level_1.findMany({
      where: { is_deleted: 0 },
      orderBy: { level_1_code: 'asc' },
      select: {
        level_1_id: true,
        level_1_code: true,
        level_1_name: true,
      },
    });

    return {
      items: items.map((item) => this.formatCategoryData(item, 1)),
    };
  }

  async getChildCategories(
    parentId: bigint,
    parentLevel: number,
  ): Promise<CategoryResponse> {
    const level = parentLevel + 1;
    if (level > 6) {
      return { items: [] };
    }

    const items = await this.prisma[`level_${level}`].findMany({
      where: {
        [`level_${parentLevel}_id`]: parentId,
        is_deleted: 0,
      },
      orderBy: { [`level_${level}_code`]: 'asc' },
    });

    return {
      items: items.map((item) => this.formatCategoryData(item, level)),
    };
  }

  async getLevel2Categories(level1Id: bigint) {
    const categories = await this.prisma.level_2.findMany({
      where: {
        level_1_id: level1Id,
        is_deleted: 0,
      },
      orderBy: { level_2_code: 'asc' },
      select: {
        level_2_id: true,
        level_2_code: true,
        level_2_name: true,
        level_1_id: true,
      },
    });
    return categories.map((category) => ({ ...category, level: 2 }));
  }

  async getLevel3Categories(level2Id: bigint) {
    return this.prisma.level_3.findMany({
      where: {
        level_2_id: level2Id,
        is_deleted: 0,
      },
      orderBy: { level_3_code: 'asc' },
    });
  }

  async getLevel4Categories(level3Id: bigint) {
    return this.prisma.level_4.findMany({
      where: {
        level_3_id: level3Id,
        is_deleted: 0,
      },
      orderBy: { level_4_code: 'asc' },
    });
  }

  async getLevel5Categories(level4Id: bigint) {
    return this.prisma.level_5.findMany({
      where: {
        level_4_id: level4Id,
        is_deleted: 0,
      },
      orderBy: { level_5_code: 'asc' },
    });
  }

  async getLevel6Categories(level5Id: bigint) {
    return this.prisma.level_6.findMany({
      where: {
        level_5_id: level5Id,
        is_deleted: 0,
      },
      orderBy: { level_6_code: 'asc' },
    });
  }

  async getCategoryPath(params: {
    level1Id?: bigint;
    level2Id?: bigint;
    level3Id?: bigint;
    level4Id?: bigint;
    level5Id?: bigint;
    level6Id?: bigint;
  }) {
    const path = [];
    const { level1Id, level2Id, level3Id, level4Id, level5Id, level6Id } =
      params;

    if (level1Id) {
      const level1 = await this.prisma.level_1.findFirst({
        where: { level_1_id: level1Id, is_deleted: 0 },
        select: {
          level_1_id: true,
          level_1_code: true,
          level_1_name: true,
        },
      });
      if (level1) {
        path.push({
          id: level1.level_1_id.toString(),
          code: level1.level_1_code,
          name: level1.level_1_name,
          level: 1,
        });
      }
    }

    if (level2Id) {
      const level2 = await this.prisma.level_2.findFirst({
        where: { level_2_id: level2Id, is_deleted: 0 },
        select: {
          level_2_id: true,
          level_2_code: true,
          level_2_name: true,
        },
      });
      if (level2) {
        path.push({
          id: level2.level_2_id.toString(),
          code: level2.level_2_code,
          name: level2.level_2_name,
          level: 2,
        });
      }
    }

    if (level3Id) {
      const level3 = await this.prisma.level_3.findFirst({
        where: { level_3_id: level3Id, is_deleted: 0 },
        select: {
          level_3_id: true,
          level_3_code: true,
          level_3_name: true,
        },
      });
      if (level3) {
        path.push({
          id: level3.level_3_id.toString(),
          code: level3.level_3_code,
          name: level3.level_3_name,
          level: 3,
        });
      }
    }

    if (level4Id) {
      const level4 = await this.prisma.level_4.findFirst({
        where: { level_4_id: level4Id, is_deleted: 0 },
        select: {
          level_4_id: true,
          level_4_code: true,
          level_4_name: true,
        },
      });
      if (level4) {
        path.push({
          id: level4.level_4_id.toString(),
          code: level4.level_4_code,
          name: level4.level_4_name,
          level: 4,
        });
      }
    }

    if (level5Id) {
      const level5 = await this.prisma.level_5.findFirst({
        where: { level_5_id: level5Id, is_deleted: 0 },
        select: {
          level_5_id: true,
          level_5_code: true,
          level_5_name: true,
        },
      });
      if (level5) {
        path.push({
          id: level5.level_5_id.toString(),
          code: level5.level_5_code,
          name: level5.level_5_name,
          level: 5,
        });
      }
    }

    if (level6Id) {
      const level6 = await this.prisma.level_6.findFirst({
        where: { level_6_id: level6Id, is_deleted: 0 },
        select: {
          level_6_id: true,
          level_6_code: true,
          level_6_name: true,
        },
      });
      if (level6) {
        path.push({
          id: level6.level_6_id.toString(),
          code: level6.level_6_code,
          name: level6.level_6_name,
          level: 6,
        });
      }
    }

    return path;
  }
}
